package com.zheng.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.zheng.mall.dao.HomeDao;
import com.zheng.mall.dto.FlashPromotionProduct;
import com.zheng.mall.dto.HomeContentResult;
import com.zheng.mall.dto.HomeFlashPromotion;
import com.zheng.mall.mapper.*;
import com.zheng.mall.model.*;
import com.zheng.mall.service.HomeService;
import com.zheng.mall.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

public class HomeServiceImpl implements HomeService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper promotionSessionMapper;
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private CmsSubjectMapper subjectMapper;

    @Override
    public HomeContentResult content() {
        HomeContentResult result = new HomeContentResult();
        //获取首页广告
        result.setAdvertiseList(getHomeAdvertiseList());
        //获取推荐品牌
        result.setBrandList(homeDao.getRecommendBrandList(0, 6));
        //获取秒杀信息
        result.setHomeFlashPromotion(getHomeFlashPromotion());
        //获取新品推荐
        result.setNewProductList(homeDao.getNewProductList(0, 4));
        //获取人气推荐
        result.setHotProductList(homeDao.getHotProductList(0,4));
        //获取推荐专题
        result.setSubjectList(homeDao.getRecommendSubjectList(0,4));
        return result;
    }

    private HomeFlashPromotion getHomeFlashPromotion() {
        HomeFlashPromotion promotion = new HomeFlashPromotion();
        Date now = new Date();
        SmsFlashPromotion flashPromotion = getFlashPromotion(now);
        if (flashPromotion != null) {
            //获取当前活动
            SmsFlashPromotionSession session = getFlashPromotionSession(now);
            if (session != null) {
                promotion.setStartTime(session.getStartTime());
                promotion.setEndTime(session.getEndTime());
                //获取下场活动
                SmsFlashPromotionSession nextSession = getNextFlashPromotionSession(promotion.getStartTime());
                if(nextSession!=null){
                    promotion.setNextStartTime(nextSession.getStartTime());
                    promotion.setNextEndTime(nextSession.getEndTime());
                }
                //秒杀商品
                List<FlashPromotionProduct> products = homeDao.getFlashProductList(flashPromotion.getId(), session.getId());
                promotion.setProductList(products);
            }
        }
        return promotion;
    }

    //根据时间获取秒杀活动
    private SmsFlashPromotion getFlashPromotion(Date date) {
        Date currDate = DateUtil.getDate(date);
        SmsFlashPromotionExample example = new SmsFlashPromotionExample();
        example.createCriteria()
                .andStatusEqualTo(1)
                .andStartDateLessThanOrEqualTo(currDate)
                .andEndDateLessThanOrEqualTo(currDate);
        List<SmsFlashPromotion> promotions = flashPromotionMapper.selectByExample(example);
        if (!promotions.isEmpty()) {
            return promotions.get(0);
        }
        return null;
    }

    //获取秒杀活动场次
    private SmsFlashPromotionSession getFlashPromotionSession(Date now) {
        Date currTime = DateUtil.getTime(now);
        SmsFlashPromotionSessionExample sessionExample = new SmsFlashPromotionSessionExample();
        sessionExample.createCriteria()
                .andStartTimeLessThanOrEqualTo(currTime)
                .andEndTimeGreaterThanOrEqualTo(currTime);
        List<SmsFlashPromotionSession> promotionSessionList = promotionSessionMapper.selectByExample(sessionExample);
        if (!CollectionUtils.isEmpty(promotionSessionList)) {
            return promotionSessionList.get(0);
        }
        return null;
    }

    //获取下场秒杀活动
    private SmsFlashPromotionSession  getNextFlashPromotionSession(Date date) {
        SmsFlashPromotionSessionExample example = new SmsFlashPromotionSessionExample();
        example.createCriteria()
                .andStartTimeEqualTo(date);
        example.setOrderByClause("start_time asc");
        List<SmsFlashPromotionSession> promotionSessions = promotionSessionMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(promotionSessions)) {
            return promotionSessions.get(0);
        }
        return null;
    }

    private List<SmsHomeAdvertise> getHomeAdvertiseList() {
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        example.createCriteria().andTypeEqualTo(1).andStatusEqualTo(1);
        example.setOrderByClause("sort desc");
        return advertiseMapper.selectByExample(example);
    }

    @Override
    public List<PmsProduct> recommendProductList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria()
                .andDeleteStatusEqualTo(0)
                .andPublishStatusEqualTo(1);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<PmsProductCategory> getProductCateList(Long parentId) {
        PmsProductCategoryExample example = new PmsProductCategoryExample();
        example.createCriteria()
                .andShowStatusEqualTo(1)
                .andParentIdEqualTo(parentId);
        example.setOrderByClause("sort desc");
        return productCategoryMapper.selectByExample(example);
    }

    @Override
    public List<CmsSubject> getSubjectList(Long cateId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andShowStatusEqualTo(1);
        if(cateId!=null){
            criteria.andCategoryIdEqualTo(cateId);
        }
        return subjectMapper.selectByExample(example);
    }

    @Override
    public List<PmsProduct> hotProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeDao.getHotProductList(offset, pageSize);
    }

    @Override
    public List<PmsProduct> newProductList(Integer pageNum, Integer pageSize) {
        int offset = pageSize * (pageNum - 1);
        return homeDao.getNewProductList(offset, pageSize);
    }
}
